<template>
  <form class="registration-form">
    <view class="form-container">
      <view class="form-title">
        注册账号
      </view>

      <view class="input-group">
        <view class="input-field">
          <input v-model="phone" maxlength="11" placeholder="请输入手机号码" @blur="validPhone"/>
          <view class="country-selector">
            <text class="country-info">+86</text>
            <uni-icons class="icon" type="down" size="35rpx" color="#7a7777" @click=""/>
          </view>
        </view>
        <view class="code">
          <input name="code" v-model="code" maxlength="6" placeholder="请输入验证码"/>
          <text class="get-code" @click="sendCode()">{{ codeMsg }}</text>
        </view>
      </view>

      <view class="button-container" @click="register()">
        <text class="next-step">下一步</text>
      </view>
      <text class="verification-hint">收不到验证码？</text>
      <view class="agreement-section">
        <view class="agreement" @click="checkeAccept = !checkeAccept">
          <uni-icons v-if="!checkeAccept" type="circle"></uni-icons>
          <uni-icons v-if="checkeAccept" color="red" type="checkbox-filled"></uni-icons>
          <text class="text">已阅读并同意</text>
        </view>
        <text class="link" @click="showArctile()">《用户协议》和《隐私政策》</text>
      </view>
    </view>
  </form>
</template>

<script>
// 注册账号的接口
import {_registerNewUser, _register, _registercode, _logincode} from '@/service/user.js';
import UniIcons from "@/uni_modules/uni-icons/components/uni-icons/uni-icons.vue";

export default {
  components: {UniIcons},
  data() {
    return {
      phone: '',
      code: '',
      checkeAccept: false,
      codeMsg: '获取验证码',
      judgecode: '',
      isCounting: false,
      timeRemaining: 60
    };
  },
  methods: {
    // 确认是否授权
    judgeAgree() {
      if (!this.checkeAccept) {
        this.$api.msg('请先确认授权');
        return false;
      }
      return true;
    },
    // 验证手机号码
    validPhone() {
      let valid = false;
      let rule = /^1[3456789]\d{9}$/;
      if (this.phone === '') {
        this.$api.msg('手机号码不能为空');
      } else if (!rule.test(this.phone)) {
        this.$api.msg('请输入正确的手机号码');
      } else {
        valid = true;
      }
      return valid;
    },
    // 验证是否为空
    validIsEmpty() {
      if (this.code === '') {
        this.$api.msg(`验证码不能为空`);
        return false;
      }
      return true;
    },
    // 验证码是否正确
    async validIsTrue() {
      let res = await _register({
        account_user: this.phone,
        verifyCode: this.code
      });
      console.log(res);
      if (res.flag == false) {
        this.$api.msg(res.message);
        this.judgecode = false;
      } else {
        this.judgecode = true;
      }
    },
    async register() {
      if (this.judgeAgree()) {
        if (this.validPhone() && this.validIsEmpty()) {
          this.validIsTrue();
          if (this.judgecode) {
            uni.navigateTo({
              url: '/pages/register/registerComplete?phone=' + this.phone
            });
          }
        }
      }
    },
    timewait() {
      this.isCounting = true;
      this.codeMsg = this.timeRemaining + '秒后重新获取'; // 更新提示信息
      const countdown = setInterval(() => {
        this.timeRemaining--;
        this.codeMsg = this.timeRemaining + '秒后重新获取'; // 更新剩余时间提示

        if (this.timeRemaining <= 0) {
          clearInterval(countdown); // 清除计时器
          this.codeMsg = '获取验证码'; // 恢复按钮提示
          this.isCounting = false; // 倒计时结束
          this.timeRemaining = 60;
        }
      }, 1000);
    },
    async sendCode() {
      //验证手机号码是否合法
      if (!this.validPhone() || this.isCounting) {
        return;// 如果手机号不对或正在倒计时则不执行
      }
      let res = await _registercode({
        account_user: this.phone,
      })
      this.$api.msg(res.message)
      console.log(res)
      this.timewait()
    },
    showArctile() {
      uni.navigateTo({
        url: '/pages/agreement/agreement'
      });
    }
  }
};
</script>

<style scoped lang="scss">
.registration-form {
  background-color: #ffffff;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow-y: auto;

  .form-container {
    padding: 50.58rpx 45.83rpx;
    flex: 1;
    display: flex;
    flex-direction: column;

    .form-title {
      margin-bottom: 50rpx;
      font-size: 50rpx;
      font-weight: bold;
      color: #333;
      text-align: center;
      margin-top: 80rpx;
    }

    .phone-input-group {
      display: flex;
      align-items: center;
      margin-top: 38.75rpx;
      padding: 33.33rpx 20rpx;
      background-color: #a6a6a640;
      border-radius: 25rpx;

      .phone-prefix {
        display: flex;
        align-items: center;
      }

      .prefix-icon {
        width: 50rpx;
        height: 50rpx;
        margin-left: 16.67rpx;
      }

      .prefix-code {
        font-size: 36rpx;
        color: #a6a6a6;
        margin-left: 25rpx;
      }

      .phone-input {
        flex: 1;
        font-size: 36rpx;
        color: #a6a6a6;
        margin-left: 25rpx;
      }
    }

    .code-input-group {
      display: flex;
      justify-content: space-between;
      margin-top: 38.75rpx;
      padding: 33.33rpx 20rpx;
      background-color: #a6a6a640;
      border-radius: 25rpx;

      .code-input {
        flex: 1;
        font-size: 36rpx;
        color: #a6a6a6;
      }

      .send-code-button {
        font-size: 36rpx;
        color: #138f87;
        margin-left: 16.67rpx;
        cursor: pointer;
      }
    }

    .agreement-section {
      margin-top: 100rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 25rpx;

      .agreement {
        display: flex;
        align-items: center;
        padding: 20rpx;
        color: #333232;

        .text {
          margin-left: 20rpx;
        }
      }

      .link {
        color: #0B97C4;
      }
    }

    .button-container {
      width: 100%;
      padding: 30rpx 0;
      background-color: #138f87;
      border-radius: 55rpx;
      text-align: center;
      margin-top:100rpx;
      .next-step {
        color: #ffffff;
        font-size: 37.5rpx;
        line-height: 33rpx;
      }

      &:active {
        background-color: #0e8383;
      }
    }

    .verification-hint {
      margin-top: 85rpx;
      color: #a6a6a6;
      font-size: 35rpx;
      text-align: center;
    }
  }
}

.input-group {
  width: 100%;
  margin-top: 20rpx;
  display: flex;
  flex-direction: column;
  align-items: flex-end;

  .input-field {
    display: flex;
    justify-content: space-between;
    width: 100%;
    margin-top: 80rpx;
    padding: 20rpx 0 20rpx;
    border-bottom: solid #138f8780;

    input {
      width: 100%;
      color: #7a7777;
      font-size: 37.5rpx;
    }

    .country-selector {
      display: flex;
      align-items: center;
    }

    .country-info {

    }
  }

  .code {
    display: flex;
    justify-content: space-between;
    width: 100%;
    margin-top: 80rpx;
    padding: 20rpx 0 20rpx;
    border-bottom: solid #138f8780;

    .get-code {
      color: #138f87;
      cursor: pointer;
    }

    input {
      width: 100%;
      color: #7a7777;
      font-size: 37.5rpx;
    }
  }
}
</style>
